package com.myhome.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author QYiXin
 * @Date 2021 10 10 12 06
 **/
@Component
public class AutoFillHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        String id = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        Long userId = Long.valueOf(id);
        this.strictInsertFill(metaObject, "createBy", Long.class, userId);
        this.strictInsertFill(metaObject, "created", Date.class, new Date());
        this.strictInsertFill(metaObject, "lastUpdateTime", Date.class,new Date());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        String id = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        Long userId = Long.valueOf(id);
        this.strictUpdateFill(metaObject, "modifyBy", Long.class, userId);
        this.strictUpdateFill(metaObject, "lastUpdateTime", Date.class, new Date());
    }
}
